package org.cqut.system.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lkx.util.ExcelUtil;
import org.cqut.framework.security.utils.ResponseUtils;
import org.cqut.framework.web.response.ResultInfo;
import org.cqut.framework.web.response.Status;
import org.cqut.system.user.domain.SysUser;
import org.cqut.system.user.dto.UserExcelDTO;
import org.cqut.system.user.service.SysUserService;
import org.cqut.system.user.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * @author XiAoXu
 * @date 2021/10/7 16:35
 * @description
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private final SysUserService userService;

    public UserController(SysUserService userService) {
        this.userService = userService;
    }

    @GetMapping("/details")
    public ResponseUtils getUserDetails(String username) {
        SysUser sysUser = userService.findUserByUserName(username);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(sysUser, userVo);
        return ResponseUtils.success(userVo);
    }

    @GetMapping("/list")
    public ResultInfo userList(@RequestParam("page") int page, @RequestParam("limit") int limit, @RequestParam("condition") String condition){
        IPage<UserVo> list = userService.getStudentList(page, limit, condition);
        if (Objects.nonNull(list)){
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code,Status.QUERY_DETAILS_SUCCESS.message,list);
        }else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code,Status.QUERY_DETAILS_ERROR.message);
        }
    }

    @PostMapping("/add")
    public ResultInfo addStudent(@RequestBody SysUser user){
        boolean flag = userService.insertStudent(user);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @PostMapping("/edit")
    public ResponseUtils editUser(@RequestBody UserVo userVo) {
        boolean flag = userService.editUser(userVo);
        if (flag) {
            return ResponseUtils.success(null);
        } else {
            return ResponseUtils.fail(null);
        }
    }

    @PostMapping(path = "/update/nickname")
    public ResultInfo updateNickname(@RequestBody UserVo userVo) {
        boolean flag = userService.updateNickname(userVo);
        if (flag) {
            return new ResultInfo(Status.UPDATE_DETAILS_SUCCESS.code, Status.UPDATE_DETAILS_SUCCESS.message, userVo);
        } else {
            return new ResultInfo(Status.UPDATE_DETAILS_ERROR.code, Status.UPDATE_DETAILS_ERROR.message);
        }
    }

    @PostMapping(path = "/update/password")
    public ResultInfo updatePassword(@RequestBody SysUser user) {
        boolean flag = userService.updatePasswordByUsername(user);
        if (flag) {
            return new ResultInfo(Status.UPDATE_PASSWORD_SUCCESS.code, Status.UPDATE_PASSWORD_SUCCESS.message);
        } else {
            return new ResultInfo(Status.UPDATE_PASSWORD_ERROR.code, Status.UPDATE_PASSWORD_ERROR.message);
        }
    }

    @PostMapping(path = "/reset/{userId}")
    public ResultInfo resetPassword(@PathVariable String userId) {
        boolean flag = userService.resetPassword(userId);
        if (flag) {
            return new ResultInfo(Status.UPDATE_PASSWORD_SUCCESS.code, Status.UPDATE_PASSWORD_SUCCESS.message);
        } else {
            return new ResultInfo(Status.UPDATE_PASSWORD_ERROR.code, Status.UPDATE_PASSWORD_ERROR.message);
        }
    }

    @PostMapping(value = "/import")
    public ResultInfo importUser(MultipartFile file) throws Exception {
        String msg = userService.importUser(file);
        if (Objects.equals("", msg)) {
            return new ResultInfo(Status.UPDATE_PASSWORD_SUCCESS.code, Status.UPDATE_PASSWORD_SUCCESS.message, msg);
        } else {
            return new ResultInfo(Status.UPDATE_PASSWORD_ERROR.code, Status.UPDATE_PASSWORD_ERROR.message);
        }

    }

    @PostMapping("/del/{userId}")
    public ResultInfo delete(@PathVariable String userId){
        boolean flag = userService.removeById(userId);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @PostMapping("/disable/{userId}")
    public ResultInfo disable(@PathVariable String userId){
        boolean flag =userService.disableAccount(userId);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @PostMapping("/enable/{userId}")
    public ResultInfo enable(@PathVariable String userId){
        boolean flag =userService.enableAccount(userId);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }
}
